package ui 
{
	import data.*;
	
	import events.GCEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import ui.textfield.*;
	
	/**
	 * A sprite containing a single upgrade. An upgrade id of 0 means this slot
	 * is empty
	 */
	public class GCUpgradeSprite extends Sprite 
	{
		public static const WIDTH:int = 100;
		
		/*
		 * The button needs to remember which upgrade slot id it is attached to,
		 * regardless of whether
		 */
		private var _upgradeSlotId:uint;
		private var _upgradeItemId:uint;
		private var _upgradeImage:Sprite;
		private var _isEmpty:Boolean;
		private var _upgradeText:GCTextField;
		private var _buttonModeCover:Sprite;
		
		public function GCUpgradeSprite(slotId:uint, upgradeId:uint, x:int = 0, y:int = 0) 
		{
			this._upgradeSlotId = slotId;
			this._upgradeItemId = upgradeId;
			this.x = x;
			this.y = y;
			
			this.graphics.lineStyle(5, GCConstants.COLOR_RED);
			this.graphics.beginFill(GCConstants.COLOR_GOLD, 0.8);
			this.graphics.drawRect(0, 0, WIDTH, WIDTH);
			this.graphics.endFill();
			_upgradeText = new GCTextField();
			_upgradeImage = new Sprite();
			_buttonModeCover = new Sprite();
			
			init();
		}
		
		/**
		 * Function that fills this slot with an upgrade.
		 * 
		 * @param	upgradeId
		 */
		public function fillWithUpgrade(upgradeId:uint):void
		{
			if (_upgradeImage.numChildren > 1)
			{
				_upgradeImage.removeChildAt(1);
			}
			_upgradeImage.addChild(GCResources.getResource(GCItemData.getNameFromId(upgradeId)));
			_upgradeText.text = GCItemData.getNameFromId(upgradeId) + "-\nClick to View/Remove";
			this._upgradeItemId = upgradeId;
			_isEmpty = false;
		}
		
		/**
		 * Function that sets this slot to empty.
		 */
		public function removeUpgrade():void
		{
			if (_upgradeImage.numChildren > 1)
			{
				_upgradeImage.removeChildAt(1);
			}
			_upgradeText.text = "Empty Slot-\nClick to Add";
			this._upgradeItemId = GCItemData.ITEM_NULL;
			_isEmpty = true;
		}
		
		public function get upgradeItemId():uint
		{
			return _upgradeItemId;
		}
		
		public function get upgradeSlotId():uint
		{
			return _upgradeSlotId;
		}
		
		public function get isEmpty():Boolean
		{
			return _isEmpty;
		}
		
		private function init():void
		{
			this.addEventListener(MouseEvent.CLICK, onSlotClicked);
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			_upgradeText.selectable = false;
			_upgradeImage.addChild(_upgradeText);
			
			// The slot is empty if the item given is a null id
			if (GCItemData.ITEM_NULL != _upgradeItemId)
			{
				fillWithUpgrade(_upgradeItemId);
			}
			else
			{
				removeUpgrade();
			}
			
			this.addChild(_upgradeImage);
			
			_buttonModeCover.graphics.beginFill(GCConstants.COLOR_WHITE, 0);
			_buttonModeCover.graphics.drawRect(0, 0, this.width, this.height);
			_buttonModeCover.graphics.endFill();
			_buttonModeCover.buttonMode = true;
			this.addChild(_buttonModeCover);
			
			this.alpha = 0.5;
		}
		
		private function onSlotClicked(event:MouseEvent):void
		{
			dispatchEvent(new GCEvent(GCEvent.UPGRADE_SELECTED_EVENT));
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			this.alpha = 1.0;
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			this.alpha = 0.5;
		}
	}

}